home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / opentcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-09  |  2.1 KB  |  106 lines

  1. #ifdef HAS_TCP
  2.  
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <netdb.h>
  9. #include <sys/socket.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include "lutil.h"
  14.  
  15. #define FIDOPORT 60179        /* my birthday */
  16.  
  17. extern void linedrop(int);
  18.  
  19. int opentcp(char*);
  20. void closetcp(void);
  21.  
  22. extern int h_errno;
  23.  
  24. static int fd=-1;
  25. extern int f_flags;
  26.  
  27. /* opentcp() was rewritten by Martin Junius */
  28.  
  29. int opentcp(name)
  30. char *name;
  31. {
  32.     struct servent *se;
  33.     struct hostent *he;
  34.     int a1,a2,a3,a4;
  35.     char *errmsg;
  36.     int fd;
  37.     struct sockaddr_in server;
  38.  
  39.     debug(18,"try open tcp connection to %s",name);
  40.  
  41.     server.sin_family=AF_INET;
  42.     if ((se=getservbyname("fido","tcp")))
  43.         server.sin_port=se->s_port;
  44.     else server.sin_port=htons(FIDOPORT);
  45.     if (sscanf(name,"%d.%d.%d.%d",&a1,&a2,&a3,&a4) == 4)
  46.         server.sin_addr.s_addr=inet_addr(name);
  47.     else if ((he=gethostbyname(name)))
  48.         memcpy(&server.sin_addr,he->h_addr,he->h_length);
  49.     else
  50.     {
  51.         switch (h_errno)
  52.         {
  53.         case HOST_NOT_FOUND:    errmsg="Authoritative: Host not found"; break;
  54.         case TRY_AGAIN:        errmsg="Non-Authoritive: Host not found"; break;
  55.         case NO_RECOVERY:    errmsg="Non recoverable errors"; break;
  56.         default:        errmsg="Unknown error"; break;
  57.         }
  58.         loginf("no IP address for %s: %s\n",name,errmsg);
  59.         return -1;
  60.     }
  61.  
  62.     debug(18,"trying %s at port %d with protocol %d",
  63.         inet_ntoa(server.sin_addr),(int)ntohs(server.sin_port));
  64.  
  65.     signal(SIGPIPE,linedrop);
  66.     fflush(stdin);
  67.     fflush(stdout);
  68.     setbuf(stdin,NULL);
  69.     setbuf(stdout,NULL);
  70.     close(0);
  71.     close(1);
  72.     if ((fd=socket(AF_INET,SOCK_STREAM,0)) != 0)
  73.     {
  74.         logerr("$cannot create socket (got %d, expected 0");
  75.         open("/dev/null",O_RDONLY);
  76.         open("/dev/null",O_WRONLY);
  77.         return -1;
  78.     }
  79.     if (dup(fd) != 1)
  80.     {
  81.         logerr("$cannot dup socket");
  82.         open("/dev/null",O_WRONLY);
  83.         return -1;
  84.     }
  85.     clearerr(stdin);
  86.     clearerr(stdout);
  87.     if (connect(fd,(struct sockaddr *)&server,sizeof(server)) == -1)
  88.     {
  89.         loginf("$cannot connect %s",inet_ntoa(server.sin_addr));
  90.         return -1;
  91.     }
  92.  
  93.     f_flags=0;
  94.  
  95.     loginf("connected to %s",inet_ntoa(server.sin_addr));
  96.     return 0;
  97. }
  98.  
  99. void closetcp(void)
  100. {
  101.     shutdown(fd,2);
  102.     signal(SIGPIPE,SIG_DFL);
  103. }
  104.  
  105. #endif
  106.